package com.AAL.DAO;

import java.sql.Date;
import java.util.List;

import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.ILogForStockDAO;
import com.AAL.Model.ActionOnStock;
import com.AAL.Model.LogDetailsForStock;
import com.AAL.Model.LogForStock;
import com.AAL.Model.OrderInvoice;
import com.AAL.Model.PurchaseInvoice;
import com.AAL.Model.TransferingRequest;
import com.AAL.Model.WareHouse;
import com.AAL.Utils.HibernateUtil;

public class LogForStockDAO implements ILogForStockDAO {

	@Override
	public LogForStock saveAndUpdateLogForStock(LogForStock logForStock) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(logForStock);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return logForStock;
	}
	//phat @Override
	public List<LogForStock> saveAndUpdateLogForStockArray(List<LogForStock> listLogForStocksSave) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			List<LogDetailsForStock> listLogDetailsForStocks;
			for(LogForStock item: listLogForStocksSave){
				session.saveOrUpdate(item);
				listLogDetailsForStocks = item.getLogDetailsForStocks();
				/*System.out.println("getUnitPriceOfInventory"+listLogDetailsForStocks.get(0).getUnitPriceOfInventory());
				System.out.println("getUnitPriceOfInventory"+listLogDetailsForStocks.get(0).getUnitPriceOfInventory());
				System.out.println("getUnitPriceOfInventory"+listLogDetailsForStocks.get(0).getUnitPriceOfInventory());
				System.out.println("getUnitPriceOfInventory"+listLogDetailsForStocks.get(0).getUnitPriceOfInventory());
				System.out.println("getUnitPriceOfInventory"+listLogDetailsForStocks.get(0).getUnitPriceOfInventory());
				System.out.println("getUnitPriceOfInventory"+listLogDetailsForStocks.get(0).getUnitPriceOfInventory());
				System.out.println("getUnitPriceOfInventory"+listLogDetailsForStocks.get(0).getUnitPriceOfInventory());
			*/	for(LogDetailsForStock item1: listLogDetailsForStocks){
					item1.setLogForStock(item);
					System.out.println(""+item1.getLogForStock());
					System.out.println(""+item1.getWheelDetail());
					System.out.println(""+item1.getInvoiceAmount());
					System.out.println(""+item1.getRealAmount());
					System.out.println(""+item1.getUnitPrice());
					System.out.println(""+item1.getUnitPrice());
					session.saveOrUpdate(item1);
				}
			}
			
		} catch (Exception e) {
			System.out.println("Loi:"+ e.getMessage());
			session.getTransaction().rollback();
			return null;
		}
		session.getTransaction().commit();
		return listLogForStocksSave;
	}
	//phat end

	@Override
	public LogForStock deleteLogForStock(int logForStockID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			LogForStock logForStock = (LogForStock) session.load(
					LogForStock.class, logForStockID);
			if (null != logForStock) {
				session.delete(logForStock);
			}
			session.getTransaction().commit();
			return logForStock;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
	}

	@Override
	public LogForStock getLogForStock(int logForStockID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		LogForStock logForStock = (LogForStock) session.load(LogForStock.class,
				logForStockID);
		return logForStock;
	}

	@Override
	public List<LogForStock> getLstLogForStockByWarehouse(WareHouse warehouse,
			Date fromDate, Date toDate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<LogForStock> getLstLogForStockByWarehouse(WareHouse warehouse,
			Date fromDate, Date toDate, ActionOnStock action) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<LogForStock> getLstLogForStock(OrderInvoice orderInvoice,
			PurchaseInvoice purchaseInvoice,
			TransferingRequest transferingRequest) {
		// TODO Auto-generated method stub
		return null;
	}

	

}
